const { con, Result } = require('./common');
const { Getusersig } = require('./tim_mod.js');
const http = require('http');
const url = require('url');

let arr = 'https://console.tim.qq.com/v4/im_open_login_svc/account_check?usersig=eJwtzMEKgkAUheF3mW1h15m5TiO0CdrEUJFSFG6KmeRmhqiZEL17pi7Pd*D-sNhEXuNKFjLuAZv2m6x71nSjnn2hhFZaAOJ4Vza7FAVZFvoSQOJcKjE8ri2odJ0jIgeAQWvK-xaACpBrn48VSrv6af3a5sns3O74EfeVukNExuhG6okwzSZLzfsaLx8H4-LVgn1-svExrQ__&identifier=13739793055&sdkappid=1400458473&contenttype=json';


let User_register = async (user_phone, user_password, password_confim) => {
	let res = new Result();
	let sql1 = `select * from user where user_phone = ${user_phone}`;
	let sql2 = `select * from business where business_phone = ${user_phone}`;
	let [rows1] = await con.execute(sql1);
	let [rows2] = await con.execute(sql2);
	let date = new Date();
	let user_sig = Getusersig(user_phone);
	let time = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
	if (rows1.length == 0 && rows2.length == 0) {
		let sql3 = `select * from user`;
		let [rows3] = await con.execute(sql3);
		if (user_password == password_confim) {
			if (rows3.length == 0) {
				let sql4 = `insert user values(1,1,'a.png',${user_phone},'xxx','${user_password}','xxx'})`;
				let [rows4] = await con.execute(sql4);
			} else {
				let sql4 = `insert user values(${rows3[rows3.length - 1].user_id + 1},'${rows3[rows3.length - 1].user_id + 1}','a.png',${user_phone},'xxx','${user_password}','xxx','${user_sig}','${time}',false)`;
				let [rows4] = await con.execute(sql4);
			}
		} else {
			res.code = 40003;
			res.msg = '两次密码不一致'
		}
	} else {
		res.code = 40004;
		res.msg = '该手机号已被注册';
	}
	return res
}
let User_get_slg = async (user_phone, user_password) => {
	let res = new Result()
	let sql = `select * from user where user_phone = '${user_phone}'`
	let [rows] = await con.execute(sql)
	if (rows.length === 0) {
		res.msg = '该手机号未注册'
		res.code = 40001
	} else if (rows[0].user_password !== user_password) {
		res.msg = '密码错误'
		res.code = 40002
	} else {
		res.data = rows[0]
	}
	return res
}
let Look = async () => {
	let res = new Result()
	let sql = `select * from user`;
	let [rows] = await con.execute(sql)
	res.data = rows;
	return res
}

let Modify = async (user_id, user_name, file, user_phone, user_email, user_password, password_confim, user_sign, password) => {
	let res = new Result()
	if (user_sign) {
		let sql = `update user set user_sign = '${user_sign}' where user_id = ${user_id}`;
		let [rows] = await con.execute(sql);
	}
	if (user_name) {
		let sql = `update user set user_name = '${user_name}' where user_id = ${user_id}`;
		let [rows] = await con.execute(sql);
	}
	if (file) {
		let sql = `update user set user_pic = '${file}' where user_id = ${user_id}`
		let [rows] = await con.execute(sql);
		let sql1 = `update image set image_name = '${file}' where useri_id = ${user_id}`;
		let [rows1] = await con.execute(sql1);
	}
	if (user_phone) {
		let sql = `select * from user where user_id = ${user_id}`;
		let [rows] = await con.execute(sql);
		if (rows[0].user_password == password) {
			let sql1 = `select * from user where user_phone = ${user_phone}`;
			let [rows1] = await con.execute(sql1);
			let sql2 = `select * from business where business_phone = ${user_phone}`;
			let [rows2] = await con.execute(sql2);
			if (rows1.length == 0 && rows2.length == 0) {
				let sql3 = `update user set user_phone = ${user_phone} where user_id = ${user_id}`;
				let [rows3] = await con.execute(sql3);
			} else {
				res.code = 40004;
				res.msg = '该手机号已被注册';
				return res;
			}
		} else {
			res.code = 40002;
			res.msg = '密码错误';
			return res;
		}
	}
	if (user_email) {
		let sql = `select * from user where user_id = ${user_id}`;
		let [rows] = await con.execute(sql);
		if (rows[0].user_password == password) {
			let sql1 = `select * from user where user_email = ${user_email}`;
			let [rows1] = await con.execute(sql1);
			let sql2 = `select * from business where business_email = ${user_email}`;
			let [rows2] = await con.execute(sql2);
			if (rows1.length == 0 && rows2.length == 0) {
				let sql3 = `update user set user_email = ${user_email} where user_id=${user_id}`;
				let [rows3] = await con.execute(sql3);
			} else {
				res.code = 40006;
				res.msg = '该邮箱已被注册';
				return res;
			}
		} else {
			res.code = 40002;
			res.msg = '密码错误';
			return res;
		}
	}
	if (user_password) {
		let sql = `select * from user where user_id = ${user_id}`;
		let [rows] = await con.execute(sql);
		if (rows[0].user_password == password) {
			if (user_password == password_confim) {
				let sql1 = `update user set user_password = '${user_password}' where user_id = ${user_id}`;
				let [rows1] = await con.execute(sql1);
			} else {
				res.code = 40003;
				res.msg = '两此输入密码不一致';
				return res;
			}
		} else {
			res.code = 40002;
			res.msg = '密码错误';
			return res;
		}
	}
	let sqll = `select * from user where user_id = ${user_id}`;
	let [rows] = await con.execute(sqll);
	res.data = rows;
	return res;
}
let DeleteUser = async (admin_id, admin_password, user_id, user_password) => {
	let res = new Result();
	console.log(user_id,user_password);
	if (user_id) {
		let sql7 = `select * from user where user_id = ${user_id}`;
		[rows7] = await con.execute(sql7);
		console.log(rows7);
	}
	if (admin_id) {
		let sql9 = `select * from admin where admin_id = ${admin_id} `;
		[rows9] = await con.execute(sql9);
	}
	console.log(rows7);
	if (rows7.length == 1 || rows9.length == 1) {
		let sql = `delete from collection where user_id = ${user_id}`;
		let [rows] = await con.execute(sql);
		let sql1 = `delete from appointment where usera_id = ${user_id}`;
		let [rows1] = await con.execute(sql1);
		let sql2 = `delete from comment where userco_id = ${user_id}`;
		let [rows2] = await con.execute(sql2);
		let sql3 = `select * from dynamic where userd_id = ${user_id}`;
		let [rows3] = await con.execute(sql3);
		rows3.map(async (item) => {
			let sql = `delete from dynamic_temp_pic where dynamic_temp_id = ${item.id}`;
			let [rows] = await con.execute(sql);
			let sql1 = `delete from dynamic_pic where dynamic_id = ${item.id}`;
			let [rows1] = await con.execute(sql1);
		})
		let sql5 = `delete from dynamic where userd_id = ${user_id}`;
		let [rows5] = await con.execute(sql5);
		let sql8 = `delete from image where useri_id = ${user_id}`;
		let [rows8] = await con.execute(sql8);
		let sql6 = `delete from user where user_id = ${user_id}`;
		let [rows6] = await con.execute(sql6);
		let opt = {
			url: arr,
    			method: 'POST',
    			json:true,
    			body: { "CheckItem":[{"UserID":`${rows7[0].user_phone}`}]}
		}	
		http.get(opt,(res)=>{
			let data = '';
  			res.on('data',(chunck)=>data+=chunck);
  			res.on('end',()=>{
				console.log(data);
			})
			
		})
	} else {
		res.code = 40002;
		res.msg = '密码错误';
	}
	return res;
}

const User_get_info = async (user_phone) => {
	let res = new Result();
	let sql = `select * from user where user_phone = ${user_phone}`;
	let [rows] = await con.execute(sql);
	if (rows.length == 0) {
		res.code = 40007;
		res.msg = '没有该用户';
	} else {
		res.data = rows[0];
	}
	return res;
}
const User_get_info2 = async (user_id) => {
	let res = new Result();
	let sql = `select user_name, user_pic from user where user_id = ${user_id}`;
	let [rows] = await con.execute(sql);
	if (rows.length == 0) {
		res.code = 40007;
		res.msg = '没有该用户';
	} else {
		res.data = rows[0];
	}
	return res;
}

module.exports = { User_get_slg, User_register, Look, Modify, DeleteUser, User_get_info, User_get_info2 }
